<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ArticleModel extends Model
{
    protected $table = 'article';

    //插入文章内容
    public function insertArticle($data) {
        //$res = self::insert($data); insert()不会插入时间戳字段
        $this->title = $data['title'];
        $this->cate_id = $data['cate_id'];
        $this->keywords = $data['keywords'];
        $this->author = $data['author'];
        $this->img_url = $data['img_url'];
        $this->desc = $data['desc'];
        $this->content = $data['content'];
        $res = $this->save();

        return $res;
        
    }

    //获取文章列表(联表查询)
    public function getArts() {
        $res = DB::table('article as a')
                    ->join('cate as c','a.cate_id','=','c.id')
                    ->select('a.id' , 'a.title' , 'a.img_url' , 'a.views' , 'a.updated_at' , 'c.cate_name')
                    ->orderBy('a.id' , 'desc')    //倒叙输出
                    ->paginate(5);                  //分页
        return $res;
    }


    //获取一条数据
    public function getArticle($id) {
        $res = self::find($id)->toArray();
        return $res;
    }

    //更新文章
    public function upArticle($id , $data) {
        $res = self::where('id' , $id)->update($data);
        return $res;
    }

    //删除文章
    public function delArticle($id) {
        $res = self::destroy($id);
        return $res;
    }

    //获取文章关键字
    public function getArticleKey() {
        $res = self::pluck('keywords')->toArray();
        return $res;
    }

    //获取文章列表
    public function getIndexArt() {
        $res = self::select('title' , 'desc' , 'id' , 'img_url')
                ->orderBy('id' , 'desc')
                ->paginate(5);
        return $res;
    }

    //获取最新文章
    public function getNewArt() {
        $res = self::select('title' , 'id')
                ->orderBy('id' , 'desc')
                ->take(5)   //获取5条数据
                ->get()
                ->toArray();
        return $res;
    }

    //获取点赞最多的文章
    public function getLikeArt() {
        $res = self::select('title' , 'id' , 'likes')
                ->orderBy('likes' , 'desc')
                ->take(5)
                ->get()
                ->toArray();
        return $res;
    }

    //获取文章的分类
    public function getArtByCate($id) {
        $res = self::where('cate_id' , $id)
                ->select('title' , 'desc' , 'img_url' ,'id')
                ->orderBy('id' , 'desc')
                ->paginate(2);
        return $res;
    }

    //获取文章
    public function getArt($id) {
        $res = self::find($id);
        //判断数据是否存在
        if ($res) {
            return $res->toArray();
        } else {
            return ['status' => 'fail'];
        }

    }

    //文章阅读数+1
    public function viewInc($id) {
        //自增 increment()
        self::where('id' , $id)->increment('views');
    }

    //获取上一篇文章
    public function getPreArt($id) {
        $res = self::where('id' , '<' , $id)
                    ->select('id' , 'title')
                    ->orderBy('id' , 'desc')
                    ->first();
        return $res;
    }

    //获取下一篇文章
    public function getNextArt($id) {
        $res = self::where('id', '>' , $id)
                    ->orderBy('id' , 'asc')
                    ->first();
        return $res;
    }

    //插入用户点赞
    public function insertLike($username , $id) {
        //用户id
        $user_id = DB::table('user')->where('username' , $username)->value('id');
        
        $res = DB::table('likes')->insert(['user_id' => $user_id , 'art_id' => $id]);

        if ($res) {
            self::where('id' , $id)->increment('likes');
            return self::where('id' , $id)->value('likes');
        }
    }

    //判断用户是否已经点过赞
    public function checkLike($username , $id) {
        //用户id
        $user_id = DB::table('user')->where('username' , $username)->value('id');

        $res = DB::table('likes')->where('user_id' , $user_id)->where('art_id' , $id)->first();

        // $res ?  false : true;
        if ($res) {
            return false;
        } else {
            return true;
        }
    }

    //搜索文章
    public function searchArt($keywords) {
        //左右模糊查询
        $res = self::where('title' , 'like' , '%'.$keywords.'%') //标题有关键词
                    ->orwhere('content' , 'like' , '%'.$keywords.'%') //内容有关键词
                    ->select('title' , 'id' , 'desc' , 'img_url')
                    ->orderBy('id' , 'desc')
                    ->get()
                    ->toArray();
        return $res;
    }
}
